<template>
  <el-dialog title="新增目录" :visible="showDialog" :before-close="handleClose">
    <!-- 表单 -->
    <el-form ref="addDeptRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="所属学科" prop="username">
        <el-select v-model="alist.subjectID" placeholder="请选择">
          <el-option v-for="item,index in getlaern.get" :key="index" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="目录名称" prop="mobile">
        <el-input v-model="alist.directoryName" style="width:50%" placeholder="输入目录名称
" />
      </el-form-item>

    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { simple } from '../../api/hmmm/subjects'
import { add } from '@/api/hmmm/directorys.js'
import Types from '@/api/base/baseApi.js'
export default {
  name: 'DirectorysAdd',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: {
        username: '', // 所学科目
        mobile: ''
      },
      alist: {
        subjectID: '',
        directoryName: ''
      },
      // 所属部门的加载状态
      loading: false,
      // 控制所属部门的展示和隐藏
      showTree: false,
      // 部门列表数据
      depts: [],
      // 聘用形式
      hireType: Types.hireType,
      getlaern: {
        get: []
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getlearn()
  },
  methods: {
    async getlearn () {
      const res = await simple()
      this.getlaern.get = res.data
      console.log(res)
    },
    // 添加部门提交表单
    handleSubmit () {
      this.$refs.addDeptRef.validate(async valid => {
        if (!valid) return
        // 单独验证部门
        // this.$refs.addDeptRef.validateField('departmentName')
        try {
          const ret = await add(this.alist)
          console.log(ret.status)
          if (ret.status === 200) {
            // 添加成功；关闭弹窗；刷新列表
            this.$emit('update:showDialog', false)
            // 刷新列表 this.$parent 表示当前组件的父组件实例
            // console.log(this.$parent)
            this.$parent.loadList()
            this.$message.success('添加成功')
          }
        } catch (err) {
          console.log(err)
          this.$message.error('添加失败')
        }
      })
    },
    // 关闭弹窗
    handleClose () {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 重置表单
      // this.$refs.addDeptRef.resetFields()
      this.formData = this.$options.data().formData
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
}
</style>
